package org.martyn.danbodroid.gui.activitys;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;

import org.martyn.danbodroid.DanboDroid;
import org.martyn.danbodroid.R;
import org.martyn.danbodroid.gui.ActionBar;
import org.martyn.danbodroid.gui.PreviewPicture;
import org.martyn.danbodroid.gui.adapters.PreviewPictureAdapter;
import org.martyn.danbodroid.logger.Logger;
import org.martyn.danbooru.error.DanbooruException;
import org.martyn.danbooru.types.Post;
import org.martyn.danbooru.types.PostPicture;
import org.xml.sax.SAXException;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class PostsListActivity extends BaseActivity implements OnItemClickListener
{
	private GridView pictureGridView;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.postslist_view);

		ActionBar actionBar = this.setupActionBar();
		
		actionBar.setTitle(this.getTitle());
		actionBar.setHomeButtonVisable(true);
		actionBar.setSearchButtonVisable(true);
		
		pictureGridView = (GridView) this.findViewById(R.id.pictureGridView);
		pictureGridView.setOnItemClickListener(this);
		
		DanboDroid danboDroid = (DanboDroid) this.getApplication();
		
		LoadPostsTask loadPostsTask = new LoadPostsTask();
		loadPostsTask.execute();
	}

	@Override
	public void onItemClick(
			AdapterView<?> parent,
			View view,
			int position,
			long id)
	{
		PreviewPicture previewPicture = (PreviewPicture) view;

		Logger.Verbose("", "Click "
				+ previewPicture.getPostPicture().getPost().getId());

		Intent intent = new Intent(this.getApplicationContext(),
				PostViewActvity.class);

		intent.putExtra(PostViewActvity.POST_VIEW_ACTIVITY_DATA_POST_KEY,
				previewPicture.getPostPicture().getPost());
		this.startActivity(intent);

	}

	private class LoadPostsTask extends AsyncTask<Void, Void, ArrayList<PostPicture>>
	{
		@Override
		protected void onPreExecute()
		{
			getActionBar().setRefreshButtonState(true);
		}

		@Override
		protected ArrayList<PostPicture> doInBackground(
				Void... paramArrayOfParams)
		{
			 try
			 {
				 DanboDroid danboDroid = (DanboDroid) getApplication();
				
				 ArrayList<Post> posts = danboDroid.getDanbooru().listPosts(20, 1);
				
				 ArrayList<PostPicture> postPictures = new
				 ArrayList<PostPicture>();
				
				 for (int i = 0; i < posts.size(); i++)
				 {
				 postPictures.add(new PostPicture(posts.get(i),
					 danboDroid.getDanbooru().picture(posts.get(i)
					 .getPreviewUrl())));
					
					 Logger.Verbose("", "picture " + i);
				 }
			
				 Logger.Verbose("", "klaar");
			
				 return postPictures;
			
			 }
			 catch (IOException ex)
			 {
			 Logger.Error("", "probleempje", ex);
			 }
			 catch (ParserConfigurationException ex)
			 {
			
			 Logger.Error("", "probleempje", ex);
			 }
			 catch (SAXException ex)
			 {
			
			 Logger.Error("", "probleempje", ex);
			 }
			 catch (DanbooruException ex)
			 {
			 Logger.Error("", "probleempje", ex);
			 }
			
			return null;
		}

		@Override
		protected void onPostExecute(ArrayList<PostPicture> result)
		{
			// super.onPostExecute(result);

			PreviewPictureAdapter previewPictureAdapter = new PreviewPictureAdapter(
					getApplicationContext(), result);

			pictureGridView.setAdapter(previewPictureAdapter);

			getActionBar().setRefreshButtonState(false);
		}
	}
}